﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Impl;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Model;
using CharacterTrailLibrary.Persist.BLL.Model;


namespace CharacterTrailClient.UiControl
{
    public class BomTreeControl :IBomTreeControl
    {
        private IBomHeaderPersist _bomHeaderPersist = null;
        public  BomTreeControl()
        {
            _bomHeaderPersist = InstanceFactory.GetBomHeaderPersis();
        }
        public List<BomHeaderModelIncludeParent> GetBomHeaderModelIncludeParents()
        {
            //var list = _bomHeaderPersist.GetTBomHeaderModels(" ( ChildItemNumber <>'' and ChildItemNumber is not null )");
            var list = _bomHeaderPersist.GetTBomHeaderModels(" 1=1 ");
            var listBomHeaderModelIncludeParents = new List<BomHeaderModelIncludeParent>();
            foreach (var bomHeaderModel in list)
            {
                listBomHeaderModelIncludeParents.Add(GenerateBomHeaderModelIncludeParent(bomHeaderModel));
            }

            foreach (var bomHeaderModelIncludeParent in listBomHeaderModelIncludeParents)
            {
                foreach (var bomHeaderModelIncludeParent2 in listBomHeaderModelIncludeParents)
                {
                    if (bomHeaderModelIncludeParent.ParentItemNumber == bomHeaderModelIncludeParent2.ChildItemNumber)
                    {
                        bomHeaderModelIncludeParent.Parant = bomHeaderModelIncludeParent2;
                        break;
                    }
                }
            }

            return listBomHeaderModelIncludeParents;
        }

        public List<TBomHeaderModel> GetTBomHeaderModels()
        {
            //var list = _bomHeaderPersist.GetTBomHeaderModels(" 1=1 ");
            return  _bomHeaderPersist.GetTBomHeaderModels(" 1=1 ");
        }

// ReSharper disable MemberCanBeMadeStatic.Local
        private BomHeaderModelIncludeParent GenerateBomHeaderModelIncludeParent(TBomHeaderModel bomHeaderModel)
// ReSharper restore MemberCanBeMadeStatic.Local
        {
            PropertyInfo[] propertyInfos = typeof(TBomHeaderModel).GetProperties();
            var bomHeaderModelIncludeParent = new BomHeaderModelIncludeParent();
            foreach (var propertyInfo in propertyInfos)
            {
                if (propertyInfo.CanWrite)
                {
                    propertyInfo.SetValue(bomHeaderModelIncludeParent, propertyInfo.GetValue(bomHeaderModel, null), null);
                }
            }
            return bomHeaderModelIncludeParent;
        }

        public List<TBomHeaderModel> GetTBomHeaderModelsByCondition(string condition)
        {
            IBomHeaderPersist bomHeaderPersist = new BomHeaderPersist();
            var list = bomHeaderPersist.GetTBomHeaderModels(condition);
            return list;
        }
    }
}
